<?php
/**
 * Nuwani PHP IRC Bot Framework
 * Copyright (c) 2006-2010 The Nuwani Project
 *
 * Nuwani is a framework for IRC Bots built using PHP. Nuwani speeds up bot 
 * development by handling basic tasks as connection- and bot management, timers
 * and module managing. Features for your bot can easily be added by creating
 * your own modules, which will receive callbacks from the framework.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @copyright Copyright (c) 2006-2011 The Nuwani Project
 * @author Peter Beverloo <peter@lvp-media.com>
 * @author Dik Grapendaal <dik.grapendaal@gmail.com>
 * @see http://nuwani.googlecode.com
 */

//namespace Nuwani;

/**
 * Uno Nuwani Module
 * 
 * @copyright Copyright (c) 2011 The Nuwani Project, http://nuwani.googlecode.com/
 * @author Dik Grapendaal <dik.grapendaal@gmail.com>
 * @version $Id: UnoCardStack.php 141 2011-07-23 21:47:01Z dik.grapendaal $
 * @package Modules
 */

class UnoCardStack implements Countable
{
        
        /**
         * Array of UnoCards representing a stack of cards.
         * 
         * @var array
         */
        
        private $mCards;
        
        /**
         * Adds a card to the stack.
         * 
         * @param UnoCard $card The card to add.
         */
        
        public function addCard (UnoCard $card)
        {
                $this -> mCards [] = $card;
                
                if (UnoEngine :: SETTING_SORT_CARDS)
                {
                        usort ($this -> mCards, array ('UnoCard', 'compare'));
                }
        }
        
        /**
         * Returns the number of cards in this stack.
         * 
         * Provided by the Countable interface.
         * 
         * @return integer
         */
        
        public function count ()
        {
                return count ($this -> mCards);
        }
        
        /**
         * Formats this stack of cards with the given UnoOutput object.
         * 
         * @param UnoOutput $output The class handling all Uno output.
         * @return string
         */
        
        public function format (UnoOutput $output)
        {
                $formatted = '';
                
                foreach ($this -> mCards as $card)
                {
                        $formatted .= $output -> formatCard ($card);
                        $formatted .= ' ';
                }
                
                return substr ($formatted, 0, -1);
        }
        
        /**
         * Formats a given array of UnoCard objects with the given UnoOutput
         * object.
         * 
         * @param UnoOutput $output The class handling all Uno output.
         * @param array $cards The array with UnoCard objects.
         * @return string
         */
        
        public static function formatArray (UnoOutput $output, array $cards)
        {
                $formatted = '';
                
                foreach ($cards as $card)
                {
                        $formatted .= $output -> formatCard ($card);
                        $formatted .= ' ';
                }
                
                return substr ($formatted, 0, -1);
        }
}